/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.manage.organizeRelation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.BaseController;
import shop.core.common.bean.JSONResult;
import shop.services.manage.organize.OrganizeService;
import shop.services.manage.organize.bean.Organize;
import shop.services.manage.organizeRelation.OrganizeRelationService;
import shop.services.manage.organizeRelation.bean.OrganizeRelation;

import javax.servlet.http.HttpServletRequest;

/**   
 * @类名称：OrganizeRelationAction      
 * @创建人：Ltz   
 * @创建时间：2017-07-05 下午22:42:45      
 * @版本号：1.0
 * @描述：     
 */
@Controller
@RequestMapping("/manage/organizeRelation/")
public class OrganizeRelationAction extends BaseController<OrganizeRelation> {
    private static final Logger logger = LoggerFactory.getLogger(OrganizeRelationAction.class);
    @Autowired
    private OrganizeService organizeService;
    @Autowired
    private OrganizeRelationService organizeRelationService;
    private static final String page_toList = "/manage/organizeRelation/organizeRelationList";
    public OrganizeRelationService getService() {
        return organizeRelationService;
    }

    private OrganizeRelationAction(){
        super.page_toList = page_toList;
        super.page_toAdd = null;
        super.page_toEdit = null;
    }

    /**
     *
     * @param request
     * @param organizeRelation
     * @return
     * @throws Exception
     */
    @Override
    @RequestMapping(value = "insertJson", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertJson(HttpServletRequest request, @ModelAttribute("organizeRelation") OrganizeRelation organizeRelation) throws Exception {
        jsonResult = new JSONResult();
        Organize organize=new Organize();
        organize.setId(organizeRelation.getParentID());
        organize=organizeService.selectOne(organize);//获取当前机构
        Organize rs=new Organize();
        rs.setId(organizeRelation.getSonID());
        rs=organizeService.selectOne(rs);//获取选定下级机构
        OrganizeRelation or =new OrganizeRelation();
        or.setSonID(rs.getId());
        or = organizeRelationService.selectOne(or);//根据下级机构ID查询关系表
        if(or!=null&&or.getParentID()!=organize.getId()){
            jsonResult.setMessage("您已绑定过该下级机构，请勿重复绑定！");
            jsonResult.setSuccess(false);
            return jsonResult;
        }else if(or!=null&&or.getParentID()==organize.getId()){
            jsonResult.setMessage("该下级机构已被绑定，请勿重复绑定！");
            jsonResult.setSuccess(false);
            return jsonResult;
        }else{
            getService().insert(organizeRelation);
            return jsonResult;
        }
    }

    /**
     * 删除下级机构
     * @param request
     * @param organizeRelation
     * @return
     * @throws Exception
     */
    @Override
    @RequestMapping(value = "deleteJson", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult deleteJson(HttpServletRequest request, @ModelAttribute("organizeRelation") OrganizeRelation organizeRelation) throws Exception {
        getService().delete(organizeRelation);
        JSONResult jsonResult=new JSONResult();
        return jsonResult;
    }
}
